package cn.sciento.farm.devicemanager.gateway.uplink.service.fertilizer.venturi;


import cn.sciento.farm.devicemanager.common.constant.MqttConstant;
import cn.sciento.farm.devicemanager.gateway.model.uplink.FertilizerUplinkPacket;
import cn.sciento.farm.devicemanager.gateway.uplink.service.base.AbstractStringAdapter;
import cn.sciento.farm.devicemanager.gateway.uplink.service.fertilizer.FertilizerDisposeService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service(value = MqttConstant.PROTOCOL_PREFIX + MqttConstant.PROTOCOL_FERTILIZER)
public class FertilizerDataAdapter extends AbstractStringAdapter {

    @Autowired
    private List<FertilizerDisposeService> fertilizerDisposeServiceList;

    @Override
    public void analyseStringData(String[] subTopic, String message) {
        if (subTopic.length == 3) {
            switch (subTopic[1]) {
                case MqttConstant.OFFLINE:
                    break;
                case MqttConstant.UP:
                    FertilizerUplinkPacket uplinkPacket = JSON.parseObject(message, FertilizerUplinkPacket.class);
                    handleUplinkPacket(subTopic, uplinkPacket);
                    break;
                default:
            }
        }
    }

    private void handleUplinkPacket(String[] subTopic, FertilizerUplinkPacket uplinkPacket) {
        String eui = subTopic[2];
//        if(uplinkPacket.isSensorData()){
////            LogFertilizerHistory history = new LogFertilizerHistory();
////            history.setEC(uplinkPacket.getEC());
////            history.setPH(uplinkPacket.getPH());
////            history.setInstantFlow(uplinkPacket.getInstantFlow());
////            history.setTotalFlow(uplinkPacket.getTotalFlow());
////            history.setChannelNo(uplinkPacket.getChannel());
////            for(FertilizerDisposeService service :fertilizerDisposeServiceList){
////                service.handleSensorData(eui,history);
////            }
////            return;
////        }

        for (FertilizerDisposeService service : fertilizerDisposeServiceList) {
            service.handleUplinkPacket(eui, uplinkPacket);
        }

//        if(uplinkPacket.isMainPump()){
//            for(FertilizerDisposeService service :fertilizerDisposeServiceList){
//                service.handleMainPump(eui,uplinkPacket.getChannel(),uplinkPacket.getPump());
//            }
//            return;
//        }
//
//        if(uplinkPacket.isMotor()){
//            for(FertilizerDisposeService service :fertilizerDisposeServiceList){
//                service.handleMotor(eui,uplinkPacket.getChannel(),uplinkPacket.getStirringMotor());
//            }
//            return;
//        }
//
//        if(uplinkPacket.isValve()){
//            for(FertilizerDisposeService service :fertilizerDisposeServiceList){
//                service.handleValve(eui,uplinkPacket.getChannel(),uplinkPacket.getFertilizeValve());
//            }
//            return;
//        }


    }
}
